/*
 * Created on Nov 20, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package tools.blat;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * @author cm	
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 *
 */

public class KnownGeneToPslParser {
	private BufferedReader reader;
	private boolean hasmoreseq= true;
	private EstAlignment curralign= null;

	public KnownGeneToPslParser(BufferedReader reader) throws IOException, Exception {
		this.reader= reader;
		
		readNext();
	}
	
	/* (non-Javadoc)
	 * @see java.util.Iterator#hasNext()
	 */
	public boolean hasNext() {
		return hasmoreseq;
	}

	/* (non-Javadoc)
	 * @see java.util.Iterator#next()
	 */
	public Object next() {
		try {
			return nextAlignment();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	public boolean hasMoreAlignments() {
		return hasmoreseq;
	}
	
	/**
	 * 
	 * @return
	 * @throws IOException
	 * @throws BioException
	 */
	public EstAlignment nextAlignment() throws IOException, Exception {
		if (hasmoreseq == false) 
			throw new Exception("No more sequences");
		EstAlignment tmp= curralign;
		readNext();
		
		return tmp;
	}
	
	private void readNext() throws IOException, Exception {
		String s= reader.readLine();
		if (s==null) {
			hasmoreseq= false;
		}
		else if(s.charAt(0)=='#'){
			readNext();
		}else{
			String[] l= s.split("\t");
//			if (l.length != 21) {
//				throw new Exception("Invalid line: "+s);
//			}
			
			EstAlignment align= new EstAlignment();
			align.matches= 0;		
			align.mismatches= 0;
			align.repmatches= 0;
			align.ncount= 0;
			align.qnuminsert= 0;
			align.qbaseinsert= 0;
			align.tnuminsert= 0;
			align.tbaseinsert= 0;
			
			String[] tstarts=l[8].split(",");
			String[] tends=l[9].split(",");
			
			align.blockSizes = new int[tends.length];
			align.qStarts = new int[tends.length];
			align.tStarts = new int[tends.length];
			
			for(int i=0;i<tstarts.length;i++){
				align.tStarts[i]=Integer.parseInt(tstarts[i]);
				align.blockSizes[i]=Integer.parseInt(tends[i])-align.tStarts[i];
			}
			
			align.qStarts[0]=0;
			for(int i=1;i<tstarts.length;i++){
				align.qStarts[i]=align.qStarts[i-1]+align.blockSizes[i-1];
			}
				
			align.qname= l[0];
			align.qsize= align.qStarts[align.qStarts.length-1]+align.blockSizes[align.qStarts.length-1];
			align.qstart= 0;
			align.qend= align.qsize;
			align.tname= l[1];
			align.tsize= 200000000;
			align.tstart= Integer.parseInt(l[3]);
			align.tend= Integer.parseInt(l[4]);
			
			
			align.strand[0]= '+';
			align.strand[1]= ' '; 
			
			
			curralign = align;
			hasmoreseq= true;
		}
	}

	public static void main(String[] args) throws IOException, Exception {
		BufferedReader reader= new BufferedReader(new FileReader("/tmp/refseqblat.psl"));
		KnownGeneToPslParser parser= new KnownGeneToPslParser(reader);
		while (parser.hasMoreAlignments()) {
			EstAlignment a= parser.nextAlignment();
			System.out.println(a);
		}
		
	}

	/* (non-Javadoc)
	 * @see java.util.Iterator#remove()
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
